package site.yunnong.atvris.common.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;

/**
 * 模板
 * @author zjh
 * @date 2021/8/4 20:36
 */
@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);

        // key采用String的序列化方式
        template.setKeySerializer(RedisSerializer.string());
        // hash采用string的序列化方式
        template.setHashKeySerializer(RedisSerializer.string());
        // value 序列化方式采用jackson
        template.setValueSerializer(RedisSerializer.json());
        // hash的value序列化方式采用json
        template.setHashValueSerializer(RedisSerializer.json());
        template.afterPropertiesSet();
        return template;
    }
}
